package com.cmc.cloud.cmclink.doc.service.impl.manifest;

import com.cmc.cloud.cmclink.doc.constants.errorconstant.ManifestCargoCntrErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.ManifestCargoCntrConvert;
import com.cmc.cloud.cmclink.doc.entity.ManifestCargoCntrDO;
import com.cmc.cloud.cmclink.doc.mapper.ManifestCargoCntrMapper;
import com.cmc.cloud.cmclink.doc.service.manifest.ManifestCargoCntrService;
import com.cmc.cloud.cmclink.doc.api.document.rep.manifestrep.manifestcargocntr.ManifestCargoCntrCreateReqVO;
import com.cmc.cloud.cmclink.doc.api.document.rep.manifestrep.manifestcargocntr.ManifestCargoCntrUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;


/**
 * 舱单货物箱关联 Service 实现类
 *
 * @author chenzheng
 */
@Service
@Validated
public class ManifestCargoCntrServiceImpl implements ManifestCargoCntrService {

    @Resource
    private ManifestCargoCntrMapper manifestCargoCntrMapper;

    @Override
    public void createManifestCargoCntr(ManifestCargoCntrCreateReqVO createReqVO) {
        // 插入
        ManifestCargoCntrDO manifestCargoCntr = ManifestCargoCntrConvert.INSTANCE.convert(createReqVO);
        manifestCargoCntrMapper.insert(manifestCargoCntr);
    }

    @Override
    public void updateManifestCargoCntr(ManifestCargoCntrUpdateReqVO updateReqVO) {
        // 校验存在
        validateManifestCargoCntrExists(updateReqVO.getId());
        // 更新
        ManifestCargoCntrDO updateObj = ManifestCargoCntrConvert.INSTANCE.convert(updateReqVO);
        manifestCargoCntrMapper.updateById(updateObj);
    }

    @Override
    public void deleteManifestCargoCntr(Long id) {
        // 校验存在
        validateManifestCargoCntrExists(id);
        // 删除
        manifestCargoCntrMapper.deleteById(id);
    }

    private void validateManifestCargoCntrExists(Long id) {
        if (manifestCargoCntrMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(ManifestCargoCntrErrorCodeConstants.MANIFEST_CARGO_CNTR_NOT_EXISTS);
        }
    }

    @Override
    public ManifestCargoCntrDO getManifestCargoCntr(Long id) {
        return manifestCargoCntrMapper.selectById(id);
    }

    @Override
    public List<ManifestCargoCntrDO> getManifestCargoCntrList(Collection<Long> ids) {
        return manifestCargoCntrMapper.selectBatchIds(ids);
    }

}
